/**
 * ======================================
 * 用户列表
 * author: SKY-D
 * file: index.vue
 * date: 2022/6/16 22:25
 * ======================================
 */
<template>
  <page>
    <el-row slot="where">
      <el-col :span="24" class="k-text-left k-lh-2-2">
        <el-input v-model="WHERE.name" class="k-width-150-p k-mr-10" placeholder="用户名" />

        <el-button type="primary" @click="_search">查询</el-button>
        <el-button type="info" @click="_reset">重置</el-button>
      </el-col>
    </el-row>
    <!--    功能-->
    <el-row slot="where">
      <el-col :span="24" class="k-text-left k-lh-2-2">
        <el-button v-permission="['SYSTEM:ACCOUNT:ADD']" type="primary" @click="handleCreate">添加</el-button>
        <!--        <el-button v-permission="['SYSTEM:ROLE:EXPORT']"   type="warning" @click="handleExport">导出</el-button>-->
      </el-col>
    </el-row>
    <!--    表格-->
    <el-table
      slot="table"
      ref="tb"
      v-loading="LOADING"
      :data="CLASS_TABLE_DATA"
      :size="SIZE"
      border
      :height="LIST_HEIGHT"
      style="width: 100%"
      @selection-change="_handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <template v-for="(item) in setting">
        <el-table-column
          v-if="item.key === 'action'"
          :key="item.id"
          :label="item.name"
          :prop="item.key"
          :width="item.value"
        >
          <template slot-scope="scope">
            <el-button v-permission="['SYSTEM:ACCOUNT:AREA']" @click="handleRange(scope.row)">范围</el-button>
            <el-button v-permission="['SYSTEM:ACCOUNT:EDIT']" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button v-permission="['SYSTEM:ACCOUNT:DEL']" @click="handleDel(scope.row)">删除</el-button>
          </template>

        </el-table-column>
        <el-table-column
          v-else-if="item.key === 'disable'"
          :key="item.id"
          :label="item.name"
          :prop="item.key"
          :width="item.value"
        >
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.disable"
              :active-value="0"
              :inactive-value="1"
              active-color="#13ce66"
              @change="handleDisable($event,scope.row)"
            />
          </template>

        </el-table-column>
        <el-table-column
          v-else
          :key="item.id"
          :label="item.name"
          :prop="item.key"
          :width="item.value"
        />
      </template>
    </el-table>
    <!--    分页-->
    <pagination slot="pagination" />
    <!--    弹窗-->
    <template slot="modal">
      <form-modal :visible.sync="FORM_VISIBLE" :type="FORM_TYPE" :data="FORM_DATA" @refresh="_getList" />
      <range-modal :visible.sync="rangeVisible" :data="rangeData" />
    </template>
  </page>
</template>

<script>
import list from '@/list'
import UserClass from '@/models/user-class'
import formModal from './components/form'
import rangeModal from './components/range'
import { delUser, getUserList, setDisable } from '@/api/system'
const userClass = new UserClass()
export default {
  name: 'User',
  components: { formModal, rangeModal },
  mixins: [list],
  data() {
    return {
      caseRequestURLType: '',
      setting: userClass.list(),
      WHERE: {
        caseNo: '' // 访问类型
      },
      rangeVisible: false,
      rangeData: {}
    }
  },
  created() {
    this.LIST_API = getUserList
  },
  mounted() {
  },
  methods: {
    _setClass() {
      console.log('[mixin][list]设置类')
      const list = []
      this.TABLE_DATA.forEach(v => {
        const item = new UserClass(v)
        list.push(item)
      })
      this.CLASS_TABLE_DATA = list
    },
    handleDisable(val, item) {
      console.log('设置账号状态', val, item)
      setDisable({ id: item.id, disable: val }).then(res => {
        console.log('设置结果', res)
        this.$notify.success('设置成功')
      })
    },
    handleCreate() {
      console.log('添加')
      this.FORM_TYPE = 'create'
      this.FORM_VISIBLE = true
    },
    handleRange(item) {
      console.log('编辑', item)
      this.rangeData = item
      this.rangeVisible = true
    },
    handleEdit(item) {
      console.log('编辑', item)
      this.FORM_TYPE = 'edit'
      this.FORM_DATA = item
      this.FORM_VISIBLE = true
    },
    handleDel(item) {
      console.log('删除', item)
      this.$confirm('确定要将角色删除吗？？')
        .then(_ => {
          delUser({ id: item.id }).then(res => {
            console.log('删除结果', res)
            this._showSuccess('删除成功')
            this._getList()
          })
        })
        .catch(_ => {})
    }
  }
}
</script>
